{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 步骤：\n",
    "\n",
    "### 数据准备：数据获取、数据清洗、数据变换等步骤，重点是针对分析目的，进行特征选择以及特征标准化；\n",
    "\n",
    "### 数据建模：使用k-means算法进行数据建模；\n",
    "\n",
    "### 后续分析：聚类模型的特征描述分析。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>gender</th>\n",
       "      <th>age</th>\n",
       "      <th>birthdate</th>\n",
       "      <th>create_time</th>\n",
       "      <th>death</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>女</td>\n",
       "      <td>36</td>\n",
       "      <td>1984-10-17</td>\n",
       "      <td>2021-08-25 08:47:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>女</td>\n",
       "      <td>39</td>\n",
       "      <td>1982-10-02</td>\n",
       "      <td>2022-08-14 21:42:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>女</td>\n",
       "      <td>15</td>\n",
       "      <td>2005-10-07</td>\n",
       "      <td>2021-01-23 22:37:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>男</td>\n",
       "      <td>26</td>\n",
       "      <td>1993-08-27</td>\n",
       "      <td>2020-08-20 06:08:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>男</td>\n",
       "      <td>50</td>\n",
       "      <td>1971-02-28</td>\n",
       "      <td>2021-03-26 11:06:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  gender  age  birthdate         create_time  death\n",
       "0      女   36 1984-10-17 2021-08-25 08:47:00      0\n",
       "1      女   39 1982-10-02 2022-08-14 21:42:00      0\n",
       "2      女   15 2005-10-07 2021-01-23 22:37:00      0\n",
       "3      男   26 1993-08-27 2020-08-20 06:08:00      0\n",
       "4      男   50 1971-02-28 2021-03-26 11:06:00      0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "# 读取数据\n",
    "loc_df = pd.read_csv(\"iris.csv\",encoding='ANSI',engine=\"python\")\n",
    "loc_df['birthdate'] = loc_df['birthdate'].astype('datetime64')\n",
    "loc_df['create_time'] = loc_df['create_time'].astype('datetime64')\n",
    "loc_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.特征选取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 只选两个特征：age和death\n",
    "sub_loc_df = loc_df[[\"death\",\"age\"]]\n",
    "# print(sub_loc_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x1e9ec7bd470>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQA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3NJC+UuI2nh/hp4a0QcofFe+rse55EWxS0ESRLgUUaSpMJCkCQBFoIkqbAQJEmAhSBJKiwESRIA/x98acUKt7SZkwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 数据点进行可视化\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "plt.rc('font',family='fangsong') #确保绘图中的中文正常显示\n",
    "sns.scatterplot(x=\"age\", y=\"death\", data=sub_loc_df)  # data需要Dataframe数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>death</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.468521</td>\n",
       "      <td>0.036573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.468521</td>\n",
       "      <td>0.193315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.468521</td>\n",
       "      <td>-1.060620</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.468521</td>\n",
       "      <td>-0.485900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.468521</td>\n",
       "      <td>0.768035</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      death       age\n",
       "0 -0.468521  0.036573\n",
       "1 -0.468521  0.193315\n",
       "2 -0.468521 -1.060620\n",
       "3 -0.468521 -0.485900\n",
       "4 -0.468521  0.768035"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#标准化\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "km_df = sub_loc_df\n",
    "km_df_standardize = StandardScaler().fit_transform(km_df)\n",
    "km_df_standardize = pd.DataFrame(data=km_df_standardize, columns=list(km_df.columns))\n",
    "km_df_standardize.head( )\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.k值选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEKCAYAAAASByJ7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAHkdJREFUeJzt3Xt4XPV95/H3d0Yja2zLGtuSL7oYYwy+AJYNqkuBJFwSCDSAubSkCU+SJhvSLNvAk60LpIUQQgOpUzZ0yaYhl32ySUiyzRJDuMS5EAgYUirwHWxjTMBIxpZt5KtkSTPf/UNHtiRLyCNp5szl83qsR2fOmTnzsR+PPjrndy7m7oiIiETCDiAiIrlBhSAiIoAKQUREAioEEREBVAgiIhJQIYiICKBCEBGRgApBREQAFYKIiARKwg6QjsrKSp85c2bYMURE8sqLL764y92rhnpeXhXCzJkzaWxsDDuGiEheMbM3jud52mUkIiKACkFERAIqBBERAVQIIiISUCGIi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IQIUgIiKACkFERAIqBBERAVQIIiISUCGIjICZXWFmnwg7h8hoUCGIjMyasAOIjBYVgoiIACoEkVFhZieY2b+ZWWXYWUSGSxe3ExkdVwE3uHsy7CAiw6VCEBm5DwEHgDLgYMhZRIZNu4xERu4J4B7g82EHERkJFYLIyJwOzHP3jcD7zOyssAOJDJeudioiIoC2EEREJKBCEBERQIUgIiIBFYKIiAAqBBERCagQREQEUCGIiEhAhSAiIgD8f52TUZCDMNOVAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from scipy.spatial.distance import cdist\n",
    "from sklearn.cluster import KMeans\n",
    "# 存放每次结果的误差平方和\n",
    "cost =[]\n",
    "# 尝试不同的聚类个数\n",
    "K = range(1,15)\n",
    "for k in K:\n",
    "    kmeanModel = KMeans(n_clusters=k, random_state=99)\n",
    "    kmeanModel.fit(km_df_standardize)\n",
    "    cost.append(kmeanModel.inertia_) # inertia_是所有点到其所属聚类质心的平方和\n",
    "#肘部法则可视化\n",
    "plt.xlabel( 'k')\n",
    "plt.ylabel('cost')\n",
    "plt.plot(K, cost, 'o-' )\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.模型建立"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x1e9ee6fcf28>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEKCAYAAAASByJ7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvd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nTzHaQJbU550qdc21XsN8C7M2GuoAKYBpwMom93iqyoy6AAIlLzkJibzuf7KmthsTwJMDHJHqjWVGbcy4XaLtwdFb8/SddBNxvZv8D/A/Z8xnolgJhYJ4hMdwAMJXEJFtGOOcKSEyiveScywNezJLavgZ8Pnl7DPAUWVCXmT1uZqtJDBl9mC11JV1KYjIeEkMyv+JgbXlmtjsTRSWtBdrOwzMG+DnZU9unSRzNBvAcB8fm5yTvZ8p+DvYCdgNNZM/fWkoKhD5yzp0LLHbOfc7MXgXynHNXA6vNLJLB0i4DvuScewBYQ2KMMhtqewCocM5dANRn03uWDM7zSPQSsuX9ArgfGO2cO5/EAQz/A8xzzl0H/DGDdQGsInHUzvmAH7iD7KktRmJMHjPbB7zvnLuKRK95X4/PTK+7ga87584DJpAY0s2Wv7WU9EtlEREB1EMQEZEkBYKIiAAKBBERSVIgiIgIoEAQEZEkBYKIiAAKBBERSVIgiIgIoLOdivSZc24iidM+v0/ifEi/I3Fxov3A8SRO+vYA8C0S56kZbWa/zEy1Iv2nHoJI3xWQOHPlBmAG8HfAi8mLxrSa2f3AOcBbZvYIUOqcG5exakX6SYEg0ncNJK4OdhwQInEG0LazV7adpXQmMM45t4jEmVRzEfmE0JCRSN9dA/ynmdU7584kEQhnJ0+W94dkm/dJXC93tXNuM9CcoVpF+k0ntxPpo+R1tacBHwBnAO+RuETjx8BW4Jcket3XAx8BkeRwksgnggJBZICcc9cCd5hZ2Dn3deBNM3st03WJDJSGjEQG7mXgXOdcLYnP0lsZrkfksKiHICIigI4yEhGRJAWCiIgACgQREUlSIIiICKBAEBGRJAWCiIgA8P8BgttjxxJZZTwAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 导入kmeans包\n",
    "from sklearn.cluster import KMeans\n",
    "# 训练k_means模型\n",
    "km = KMeans(n_clusters=4, random_state=99 )\n",
    "km.fit(km_df_standardize)\n",
    "# 将新标签组合到原来的数据框里面\n",
    "km_label = pd.DataFrame(km.labels_, columns=[ '新标签'])\n",
    "km_df = pd.concat( [km_df, km_label], axis=1)\n",
    "#将聚类结果可视化\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "sns.scatterplot(x=\"age\", y=\"death\", hue=\"新标签\", data=km_df, palette=\"Set1\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.聚类分析，对每一聚类进行进一步分析和描述"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>death</th>\n",
       "      <th>age</th>\n",
       "      <th>新标签</th>\n",
       "      <th>gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>36</td>\n",
       "      <td>0</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>39</td>\n",
       "      <td>0</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "      <td>3</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>26</td>\n",
       "      <td>3</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   death  age  新标签 gender\n",
       "0      0   36    0      女\n",
       "1      0   39    0      女\n",
       "2      0   15    3      女\n",
       "3      0   26    3      男\n",
       "4      0   50    0      男"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# final_df作为最后分析用的数据框\n",
    "final_df = km_df\n",
    "# KMeans聚类结果增加一列\"gender”\n",
    "final_df [\"gender\"]= loc_df[\"gender\"]\n",
    "final_df.head( )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>death</th>\n",
       "      <th>age</th>\n",
       "      <th>数目</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>新标签</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>70.0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     death   age  数目\n",
       "新标签                 \n",
       "0      0.0  44.0  20\n",
       "1      1.0  10.0   3\n",
       "2      1.0  70.0   6\n",
       "3      0.0  20.0  21"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#summary_df用于对聚类的分析，下一步求平均值\n",
    "summary_df = final_df[ [ \"death\", \"age\", \"新标签\" ]].groupby(\"新标签\" ).mean().round()\n",
    "#求每一个类别中的数目\n",
    "summary_df[ '数目']=final_df.groupby(\"新标签\").size()\n",
    "#展示\n",
    "summary_df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 利用聚类，根据不同年龄人群死亡情况进行划分，最后用平均值计算每个类，得出平均年龄在44、20的患者没有死亡数目分别为20、21，平均年龄在10、70的患者死亡了的数目分别为3、6."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
